$(document).ready(function () {
	    var areaCode=$("#areaCode").val();
 		var arr = area();
		for(var i=0; i<arr.length;i++){
			$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
		}	
		if(areaCode==undefined || areaCode==""){
			areaCode=$("#change").find("option:selected").val();
		}
		loadData(areaCode);
		$("#change").change(function () {
			areaCode=$("#change").find("option:selected").val();
       		loadData(areaCode);
   		});	
});


function loadData(areaCode){
		$.ajax({
            url: urlcore + "/api/serveRate/serveRateDetailsByAreaCode?areaCode="+areaCode,
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;
                    if(da==null){
                    	$("#fullTimeRate").val("");	
                    }else{
	                  $("#change").find("option[value='"+da.areaCode+"']").attr("selected",true);
		              $("#fullTimeRate").val(parseFloat(da.fulltimeRate)*100+"%");	
                    }

                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
	
}


function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
	                /* $('#messUrl').html(imgStr2);                  
                    $('#title').text(da.title);
                    $('#content').html(da.content);*/
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}


 function save(){
 	var fullTimeRate=$("#fullTimeRate").val().replace(/[~'!<>@#$%^&*()-+_=:]/g, "");
 	//获得地区信息
	var code = $("#change").find("option:selected").val();
	//判断是否已存在
	if(fullTimeRate==""){
		alert("请输入服务费率");
		return;
	}
		$.ajax({
            url: urlcore + "/api/serveRate/checkRateDetail",
            type: "post",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            data:JSON.stringify({
            	areaCode:code,
            }),
            success:function(data){
                if (data.success == true) {
                 saveAreaDetail(fullTimeRate,code);
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    if(confirm(data.msg)){
                    	updateAreaDetail(fullTimeRate,code);
                    }
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
 	
 	
 }
 
 //保存
function  saveAreaDetail(fullTimeRate,code){
		realfullTimeRate=parseFloat(fullTimeRate)*0.01;
		$.ajax({
            url: urlcore + "/api/serveRate/saveRates",
            type: "post",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            data:JSON.stringify({
            	fulltimeRate:realfullTimeRate,
            	areaCode:code
            }),
            success:function(data){
                if (data.success == true) {
                  alert("保存成功");
                  $("#areaCode").val(code);
                   location.reload();
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
	
}

//更新
function  updateAreaDetail(fullTimeRate,code){
		realfullTimeRate=parseFloat(fullTimeRate)*0.01;
		$.ajax({
            url: urlcore + "/api/serveRate/updateRateDetail",
            type: "post",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            data:JSON.stringify({
            	fulltimeRate:realfullTimeRate,
            	areaCode:code
            }),
            success:function(data){
                if (data.success == true) {
                  alert("更新成功");
                  $("#areaCode").val(code);
                  location.reload();
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
	
}

